/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-06-10     liuduanfei   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#define DBG_TAG "adc"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

/* ADC12_IN10 --- PC0 */
void adc1_test(void)
{
    stc_adc_init_t stcAdcInit;
    /* Set the ADC pin to analog mode. */
    stc_port_init_t stcPortInit;
    stc_adc_ch_cfg_t stcChCfg;
    uint8_t au8Adc1SaSampTime = {200};

    MEM_ZERO_STRUCT(stcPortInit);
    MEM_ZERO_STRUCT(stcAdcInit);
    MEM_ZERO_STRUCT(stcChCfg);
    stcPortInit.enPinMode = Pin_Mode_Ana;
    stcPortInit.enPullUp  = Disable;
    PORT_Init(PortC, Pin00, &stcPortInit);
    /* Enable ADC1. */
    PWC_Fcg3PeriphClockCmd(PWC_FCG3_PERIPH_ADC1, Enable);

    stcAdcInit.enResolution = AdcResolution_12Bit;
    stcAdcInit.enDataAlign  = AdcDataAlign_Right;
    stcAdcInit.enAutoClear  = AdcClren_Enable;
    stcAdcInit.enScanMode   = AdcMode_SAOnce;
    /* Initialize ADC1. */
    ADC_Init(M4_ADC1, &stcAdcInit);

    stcChCfg.u32Channel  = ADC1_CH10;
    stcChCfg.u8Sequence  = ADC_SEQ_A;
    stcChCfg.pu8SampTime = &au8Adc1SaSampTime;
    /* Add ADC channel. */
    ADC_AddAdcChannel(M4_ADC1, &stcChCfg);
    
    uint16_t val;
    ADC_PollingSa(M4_ADC1, &val, 1, 10);
    
    rt_kprintf("val = %d\n", val);
}
MSH_CMD_EXPORT(adc1_test, test the adc1);

